package stubs
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	import mx.core.IFlexModule;
	import mx.core.Singleton;
	import mx.events.FlexEvent;
	import mx.managers.ISystemManagerChildManager;
	import mx.managers.SystemManager;
	
	public class SparkSystemManagerStub extends SystemManager
	{
		public namespace mx_internal = 
			"http://www.adobe.com/2006/flex/mx/internal";
		
		use namespace mx_internal;
		
		private function enterFrameHandler(event:Event):void
		{
			if (super.currentFrame == 2)
			{
				super.removeEventListener(
					Event.ENTER_FRAME, this.enterFrameHandler);
				this.kickOff();
				trace("At frame 2");
			}
		}
		
		public override function info():Object
		{
			return { usePreloader: false,
				mainClassName: "stubs.SparkApplicationStub",
				currentDomain: ApplicationDomain.currentDomain,
					compiledLocales: [ "en_US" ],
					compiledResourceBundleNames: 
					[ "components", "core", "effects", "layout", "skins", "styles" ]
			};
		}
		
		public override function create(...params):Object
		{
			var instance:Object;
			var mainClassName:String;
			var mainClass:Class;
			
			if (params.length && !(params[0] is String))
				instance = super.create.apply(this, params); 
			else
			{
				mainClassName = !params.length ? 
					"stubs.SparkApplicationStub" : params[0];
				mainClass = super.loaderInfo.applicationDomain
					.getDefinition(mainClassName) as Class;
				if (mainClass)
				{
					instance = new mainClass();
					if (instance is IFlexModule)
						(IFlexModule(instance)).moduleFactory = this;
				}
			}
			return instance;
		}
		
		// This woas preloader_preloaderDoneHandler(event:Event):void
		// but it's private...
		private function preloaderDoneHandler():void
		{
			var application:DisplayObject = this.create() as DisplayObject;
			// There was stuff about mouseCatcher - nothing to do about it
			// it's all private.
			super.document = application;
			(application as Object).moduleFactory = this;
			trace("before addchild");
			try
			{
				super.addChild(application);
			}
			catch (whatever:*)
			{
				trace("what happened?", whatever);
			}
			
			// Dispatch the applicationComplete event from the Application
			// and then agaom from the SystemManager
			// (so that loading apps know we're done).
			trace("application added...");
			application.dispatchEvent(new FlexEvent(FlexEvent.APPLICATION_COMPLETE));
			super.dispatchEvent(new FlexEvent(FlexEvent.APPLICATION_COMPLETE));
			trace("Application must be on display list now", application.stage, application.parent);
		}		
		
//		public override function getDefinitionByName(name:String):Object
//		{
//			throw "who's calling?";
//		}
		
		// kickOff() was called too late.
		mx_internal override function docFrameHandler(event:Event = null):void { }
		// same as above.
		mx_internal override function preloader_completeHandler(event:Event):void { }
		
		mx_internal override function kickOff():void
		{
			// All this crap should really go into the second frame...
			// Generated code will bring in EmbeddedFontRegistry
			var getDefinition:Function = 
				super.loaderInfo.applicationDomain.getDefinition;
			var styleManager:Object = 
				getDefinition("mx.styles::StyleManagerImpl");
			var childManagerImpl:ISystemManagerChildManager;
			var childManagerClass:Class;
			
			Singleton.registerClass("mx.core::IEmbeddedFontRegistry",
				Class(getDefinition("mx.core::EmbeddedFontRegistry")));
			
			Singleton.registerClass("mx.styles::IStyleManager",
				Class(styleManager));
			
			Singleton.registerClass("mx.styles::IStyleManager2",
				Class(styleManager));
			
			trace(getDefinition("mx.styles::StyleManagerImpl"), "stylemanager");
			
			// Register other singleton classes.
			// Note: getDefinitionByName() will return null
			// if the class can't be found.
			
			Singleton.registerClass("mx.managers::IBrowserManager",
				Class(getDefinition("mx.managers::BrowserManagerImpl")));
			
			Singleton.registerClass("mx.managers::ICursorManager",
				Class(getDefinition("mx.managers::CursorManagerImpl")));
			
			Singleton.registerClass("mx.managers::IHistoryManager",
				Class(getDefinition("mx.managers::HistoryManagerImpl")));
			
			Singleton.registerClass("mx.managers::ILayoutManager",
				Class(getDefinition("mx.managers::LayoutManager")));
			
			Singleton.registerClass("mx.managers::IPopUpManager",
				Class(getDefinition("mx.managers::PopUpManagerImpl")));
			
			Singleton.registerClass("mx.managers::IToolTipManager2",
				Class(getDefinition("mx.managers::ToolTipManagerImpl")));
			
			Singleton.registerClass("mx.managers::IDragManager", 
				Class(getDefinition("mx.managers::DragManagerImpl")));
			
			Singleton.registerClass("mx.core::ITextFieldFactory", 
				Class(getDefinition("mx.core::TextFieldFactory")));
			childManagerClass = Class(getDefinition("mx.managers.systemClasses::ChildManager")); 
			childManagerImpl = new childManagerClass(this);
			Singleton.registerClass("mx.managers::ISystemManagerChildManager", 
				childManagerClass);
			this.registerImplementation("mx.managers::ISystemManagerChildManager",
				childManagerImpl);
			// We need this poop to initialize the styles.
			new styleManager(this);
			getDefinition("_SparkApplicationStub_Styles").init(this);
			getDefinition("stubs.SparkApplicationStub").init(this);
//			getDefinition("_SparkApplicationStub_FlexInit").init(this);
			trace("preloaderdone next");
			this.preloaderDoneHandler();
			trace("after preloader done");
		}
		
		public function SparkSystemManagerStub()
		{
			super();
			super.addEventListener(
				Event.ENTER_FRAME, this.enterFrameHandler);
		}
	}
}